package com.koala.dbUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PostgresAgent {
	
	Connection conexion;
	ResultSet res=null;
	
	String driver = "org.postgresql.Driver";
	
	String usuario;
	String pass;
	String url="jdbc:postgresql://";
	String bd;
	
	public PostgresAgent(String url,String puerto, String usuario, String pass, String bd) {
		
		this.url+=url+":"+puerto+"/"+bd;
		this.usuario=usuario;
		this.pass=pass;
		this.bd=bd;
		
	}
	
	public void Conectar(){
		
		try {
			
			Class.forName(driver);
			conexion = DriverManager.getConnection(this.url, this.usuario, this.pass);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public void EjecutarSQL(String sql){
		
		try {
			
			Statement stmt = conexion.createStatement();

			res = stmt.executeQuery(sql);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public void CerrarConexion(){
		
			
			
			try {
				if(!conexion.isClosed())
					conexion.close();
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}		
		
		
	}

	protected void finalize() throws Throwable {
					
			try{			

				if(!conexion.isClosed())
					conexion.close();		
			}
			catch (Exception e) {

			}
		
		super.finalize();
	}

	public ResultSet getRes() {
		return res;
	}
}
